<template>
  <basic-container>
    <avue-crud ref="crud"
               :option="option"
               v-model:page="page"
               :table-loading="loading"
               @on-load="onLoad"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @row-del="rowDel"
               @refresh-change="refreshChange"
               @search-reset="searchChange"
               @search-change="searchChange"
               @selection-change="selectionChange"
               v-model="form"
               :data="data">
      <template #menu-left="{type,size}">
        <el-button type="danger"
                   :size="size" :disabled="multiple"
                   icon="el-icon-delete"
                   @click="handleBatchDelete">批量删除
        </el-button>
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
import {
  ref,
} from 'vue';
import {
  getProductCateList,
  addProductCate,
  updateProductCate,
  delProductCate,
} from "/@/api/base/productCate"
import useCurrentInstance from "../../utils/useCurrentInstance";
import {listProductCateSelect} from "../../api/base/productCate";
export default {
  name: 'productCate',
  setup(props) {
    const { proxy } = useCurrentInstance();

    const option = ref({
      searchMenuSpan: 6,
      tip: false,
      border: true,
      index: false,
      viewBtn: false,
      selection: true,
      addBtn:true,
      searchLabelWidth:100,
      excelBtn:false,
      labelWidth:100,
      column: [
        {label: '编码', prop: 'encode',search:true},
        {label: 'ERP编码', prop: 'erpCode',display:false},
        {label: '商品类型', prop: 'type',search:true},
        {label: '父类', prop: 'parentName',display: false},
        {label: '父类商品',
          prop: 'pid',hide: true ,type: 'select',dataType: "number",
          dicData:()=>{return  listProductCateSelect().then(res=>{return res.data;})}
        },
        {label: '创建人', prop: 'createUserName',display:false},
        {label: '录入/更新时间', prop: 'updateTime',display:false,
          search:true,searchRange:true, type: 'date',
          format: 'YYYY-MM-DD HH:mm:ss',
          searchLabelWidth: 120,
          valueFormat: 'YYYY-MM-DD HH:mm:ss',},
      ]
    });
    const loading = ref(true);
    const multiple = ref(true);
    const data = ref([]);
    const crud = ref();
    const selectionList = ref([]);
    const queryParams = ref({});
    const form = ref({});
    const page = ref({
      pageSize: 10,
      currentPage: 1,
      total: 0

    })
    const onLoad = (pages, params = {}) => {
      loading.value = true;
      getProductCateList(Object.assign({ page: pages.currentPage,
        limit: pages.pageSize,},params, queryParams.value)).then(res => {
         const resData =  res.data;
        page.value.total = resData.total;
        data.value = resData.list;
        loading.value = false;
        selectionClear();
        console.log(res)
      })
    }
    const selectionClear = () => {
      selectionList.value = [];
    }
    const rowSave = (row, done, loading) => {
      addProductCate(row).then(() => {
        done();
        onLoad(page.value);
        proxy.$message({
          type: "success",
          message: "操作成功!"
        });
      }, error => {
        window.console.log(error);
        loading();
      });
    }
    const rowUpdate = (row, index, done, loading) => {
      updateProductCate(row).then(() => {
        done();
        onLoad(page.value);
        proxy.$message({
          type: "success",
          message: "操作成功!"
        });
      }, error => {
        window.console.log(error);
        loading();
      });
    }
    const rowDel = (row) => {
      proxy.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
          .then(() => {
            return delProductCate({id:[row.id]});
          })
          .then(() => {
            onLoad(page.value);
            proxy.$message({
              type: "success",
              message: "操作成功!"
            });
          });
    }
    const refreshChange = () => {
      onLoad(page.value, queryParams.value);
    }
    const searchChange = (params, done) => {
      if (Array.isArray(params.updateTime) && params.updateTime.length > 0){
        params.beginTime = params.updateTime[0];
        params.endTime = params.updateTime[1];
      }
      page.currentPage = 1;
      onLoad(page.value, params);
      done();
    }
    const selectionChange = (list = []) => {
      selectionList.value = list;
      multiple.value = !(list.length > 0)

    }

    const handleBatchDelete = () => {
      let ids = selectionList.value.map(item => item.id);
      proxy.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
          .then(() => {
            return delProductCate({id:ids});
          })
          .then(() => {
            onLoad(page.value);
            proxy.$message({
              type: "success",
              message: "操作成功!"
            });
          });
    }

    return {
      option,
      loading,
      data,
      page,
      form,
      queryParams,
      multiple,
      onLoad,
      rowSave,
      rowUpdate,
      rowDel,
      refreshChange,
      searchChange,
      selectionChange,
      handleBatchDelete

    }
  }
}
</script>

<style>

</style>
